package org.orbis.spikezolt;

import org.anddev.andengine.entity.primitive.Line;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.PolygonShape;

public class EndPipe extends Updater {
	SpikeZoltActivity sa;
	BitmapTextureAtlas bitmapTextureAtlas;
	Sprite lowSpr, topSpr, glassSpr;
	Body body, gbody, lbody;

	public EndPipe(SpikeZoltActivity sa, int x, int y) {
		this.sa = sa;
		
		lowSpr = new Sprite(x, y, sa.texManager.TR_endLow);
		topSpr = new Sprite(x, y, sa.texManager.TR_endTop);
		glassSpr = new Sprite(x, y, sa.texManager.TR_endGlass);

		// TOP
		Shape boxShape = new Rectangle(x + 16, y, lowSpr.getWidth() - 16, 1);
		FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f,
				0.5f);
		wallFixtureDef.restitution = 0.0f;
		wallFixtureDef.friction = sa.WALL_FRICTION;
		body = PhysicsFactory.createBoxBody(sa.mPhysicsWorld, boxShape,
				BodyType.StaticBody, wallFixtureDef);
		body.setUserData("wall");
		// BOTTOM
		boxShape = new Rectangle(x + 32, y + 48, lowSpr.getWidth() - 32, 16);
		wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
		wallFixtureDef.restitution = 0.0f;
		wallFixtureDef.friction = sa.WALL_FRICTION;
		gbody = PhysicsFactory.createBoxBody(sa.mPhysicsWorld, boxShape,
				BodyType.StaticBody, wallFixtureDef);
		gbody.setUserData("wall");
		// DIAGONAL
		Line line = new Line(x + 0, y + 64, x + 32, y + 48);
		lbody = PhysicsFactory.createLineBody(sa.mPhysicsWorld, line,
				wallFixtureDef);
		lbody.setUserData("wall");

		sa.scene.attachChild(lowSpr);
		sa.scene.attachChild(topSpr);
		sa.scene.attachChild(glassSpr);
		lowSpr.setZIndex(sa.ENDTUBE_DEPTH_LOW);
		topSpr.setZIndex(sa.ENDTUBE_DEPTH_TOP);
		glassSpr.setZIndex(sa.ENDTUBE_DEPTH_TOP);
		sa.scene.sortChildren();

	}

	@Override
	public void step() {

	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		//sa.scene.detachChild(lowSpr);
		//sa.scene.detachChild(topSpr);
		//sa.scene.detachChild(glassSpr);
		lowSpr.detachSelf();
		topSpr.detachSelf();
		glassSpr.detachSelf();
		sa.mPhysicsWorld.destroyBody(body);
		sa.mPhysicsWorld.destroyBody(gbody);
		sa.mPhysicsWorld.destroyBody(lbody);
		
	}
}
